import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

/*
 * @Sangeeta
 * @Uses:This program is used to find accuracy of the H1 heuristic table from bug_svn_m_file table and h1_score table.
 * @Table created = h1_accuarcy_1
 * @Accuracy = Accuracy=(No of bugs for which one of the files modified is present/Total No of Bugs)*100
 * */
//@Dependency
//1.h1_score_table,h1_opt.java(file used to create h1_score.java)

//@set
//1. No_of_bugids

//@deprecated..... @deprecated dont... use.....dont...use @deprecate.............
//@use h1_1_acuuracy.java
public class h1_accuracy 
{
public Connection conn =  null;	
public String dbname="2jboss";
public String userid="root";
public String passwd="123";

public String use_table = "h1_score";
ArrayList<String> bugids =  new ArrayList<String>();
public int No_of_bugids;

//Limit specifies how many record we want to retrive
public void acc_1(Connection conn,int limit)
{
	Statement stmt =  null;
	String bug_and_fname = "select distinct bugid,file_name from bug_svn_m_file"; 
	
    try 
    {
		stmt =  conn.createStatement();
        stmt.execute(bug_and_fname);
        ResultSet rs = stmt.getResultSet();
        general_fun gf = new general_fun(); 
        int count = 0;
        while(rs.next())
        {
        	String bugid = rs.getString("bugid");
        	String file_m = rs.getString("file_name");
        	String file_m_path=getFilePath(bugid,file_m,conn);
        	
        	file_m =  file_m.toLowerCase();
        	file_m =  file_m.replace(".java", "");
        	
        	file_m_path = gf.remove_slash(file_m_path);
        	file_m_path = file_m_path.replace(".java", "");
        	System.out.println("bugid = "+bugid+" file_m="+file_m+" file_m_path="+file_m_path);
        	file_m_path  =gf.remove_path_stop_words(file_m_path);
        	file_m_path =  file_m_path.toLowerCase();
        	
        	System.out.println("bugid = "+bugid+" file_m"+file_m+" file_m_path="+file_m_path);
        	compute_match_and_insert(bugid,file_m,file_m_path,limit,conn);
        	System.out.println(++count);
        	//if(bugid.equalsIgnoreCase("jbas-1101"))
      		 // intr();
        	
        }//wh
       
        if(rs!=null)
        	rs.close();
        if(stmt!=null)
        	stmt.close();
	} catch (SQLException e) 
	{ 	e.printStackTrace(); }
	
}

private void compute_match_and_insert(String bugid, String file_m,String file_m_path,int limit, Connection conn) 
{
   int match = 0;
   
   String h1_score_str  = "select * from "+ use_table+" where bugid='"+bugid+"' order by sum desc limit 0,"+limit;
   System.out.println(h1_score_str);
   
   Statement stmt =  null;
   try
   {
	    stmt =conn.createStatement();
	    stmt.execute(h1_score_str);
	    ResultSet rs = stmt.getResultSet();
	    
	    int file_match = 0;
	    int file_path_match = 0;
	    String file_ret ="";
	    String file_ret_path="";
	  	double score=0;
    	String version="dont know";
    	
	    int rank=0;
	    while(rs.next())
	    {
	    	rank++;
	    	file_ret =rs.getString("file_name"); 
	    	file_ret_path =rs.getString("file_path");
	    	file_match =f_match (file_m,file_ret);
	    	file_path_match = path_match(file_m_path,file_ret_path);//@@NOt implemented will return 0
	  
	    	
	    	if(1==file_match)
	    	{
	    		score = rs.getDouble("sum");
	    		version = rs.getString("table");
	    		break;
	    	}
	    	
	    }//wh
	    
	    if(0==file_match)
	    {
	    	rank=0; 
	    	file_ret ="Not found";
	    	file_ret_path = "Not Found";
	    	score = 0;
	    	version = "Not Found";
	    }
	    
	    //table jsust holding accuracy information, with not information about the top50 files that are retrived.
	    insert_to_h1_accuraccy_1(bugid,file_m,file_m_path,file_match,file_path_match,rank,file_ret,file_ret_path,score,version,conn);
   
	if(rs!=null)
		rs.close();
	if(stmt!=null)
		stmt.close();
   } catch (SQLException e) 
   { e.printStackTrace(); } 
   
}


private void calculate_total_accuracy1(Connection conn)
{
	Statement stmt =  null;
	String bug_str = "select  distinct bugid from h1_accuracy_1";
	double accuracy1 = 0;
	int No_of_matches = 0;
	
	try 
	{
		stmt =  conn.createStatement();
		stmt.execute(bug_str);
		ResultSet rs = stmt.getResultSet();
		while(rs.next())
		{
			String bugid =  rs.getString("bugid");
			bugids.add(bugid);
		}
			
	if(rs!=null)
		rs.close();
	if(stmt!=null)
		stmt.close();
	
	} catch (SQLException e) 
	{ e.printStackTrace(); 	}
	
	
	
	for(int  i =0;i<bugids.size();i++)
	{
		String bugid =  bugids.get(i);
		String bug_files  = "select file_match from h1_accuracy_1 where bugid = '"+bugid+"'";
		try 
		{
			stmt =  conn.createStatement();
			stmt.execute(bug_files);
			ResultSet rs = stmt.getResultSet();
			boolean  match =  false;
			while(rs.next())
			 {
				int file_match = rs.getInt("file_match");
				if(1==file_match)
				{
					match = true;
					No_of_matches++;
					break;
				}
				
			 }//while
			
			if(rs!=null)
				rs.close();
			if(stmt!=null)
				stmt.close();
		} catch (SQLException e) 
		{ e.printStackTrace(); 	}
		
	}//for
	
	//int No_of_bugids = bugids.size();
	//No_of_bugids=72;
	accuracy1 = ((No_of_matches*1.0)/No_of_bugids)*100;
	System.out.println("Accuracy1= "+accuracy1);
}

private void cal_total_acc2(Connection conn)
{
	Statement stmt =  null;
	String bug_str = "select  distinct bugid from "+use_table;
	//System.out.println("Bug str="+bug_str);
	double accuracy2 = 0;
	
	bugids.removeAll(bugids);//reset
	
	
	
	try 
	{
		stmt =  conn.createStatement();
		stmt.execute(bug_str);
		ResultSet rs = stmt.getResultSet();
		int count=0;
		while(rs.next())
		{
			String bugid =  rs.getString("bugid");
			bugids.add(bugid);
			//System.out.println("count="+(count++));
		}
			
	if(rs!=null)
		rs.close();
	if(stmt!=null)
		stmt.close();
	
	} catch (SQLException e) 
	{ e.printStackTrace(); 	}
	
	
	//System.out.println("No of bugids="+bugids.size());
	
	int no_of_files=0;
	int no_of_matches = 0;
	for(int  i =0;i<bugids.size();i++)
	 {
		String bugid =  bugids.get(i);
		String bug_files  = "select file_match from h1_accuracy_1 where bugid = '"+bugid+"'";
		try 
		{
			stmt =  conn.createStatement();
			stmt.execute(bug_files);
			ResultSet rs = stmt.getResultSet();
			boolean  match =  false;
			//int no_of_file_mod_by_bug=0;
			while(rs.next())
			 {
				int file_match = rs.getInt("file_match");
				if(1==file_match)
				{
					match = true;
					no_of_matches++;
					//break;
				}
				
				no_of_files++;
			 }//while
			
			if(rs!=null)
				rs.close();
			if(stmt!=null)
				stmt.close();
		} catch (SQLException e) 
		{ e.printStackTrace(); 	}
		
	}//for
	
	//int No_of_bugids = bugids.size();
	//No_of_bugids=72;
	//System.out.println("No of matches="+no_of_matches);
	//System.out.println("No of files="+no_of_files);
	accuracy2 = ((no_of_matches*1.0)/no_of_files)*100;
	System.out.println("Accuracy2[RECALL]= "+accuracy2);
}

private void insert_to_h1_accuraccy_1(String bugid, String file_m, String file_m_path, int file_match, int file_path_match, int rank,
		String file_ret, String file_ret_path, double score, String version, Connection conn) 
{
	Statement stmt =  null;
	
	String insert_q = "insert into h1_accuracy_1 values('"+bugid+"','"+file_m+"','"+file_m_path+"',"+file_match+","+file_path_match+
	                  ","+rank+",'"+file_ret+"','"+file_ret_path+"',"+score+",'"+version+"')";
	System.out.println("insert ="+insert_q);
	try 
	  {
		   stmt =  conn.createStatement();
		   stmt.executeUpdate(insert_q);
		   
		   if(stmt!=null)
			   stmt.close();
	  } catch (SQLException e) 
	  {	e.printStackTrace(); }	
  	
	  
}//

private int path_match(String file_m_path, String file_ret_path) 
{
	
	return 0;
}

private int f_match(String file_m, String file_ret) 
{
    int match =0;
	
	System.out.println("file_m="+file_m);
	System.out.println("file_ret="+file_ret);
	
	file_m=  file_m.toLowerCase();
	file_ret =  file_ret.toLowerCase();
	if(file_m.equalsIgnoreCase(file_ret))
	{
		match=1;
	}
	
	return match;
}

private String getFilePath(String bugid, String file_m,Connection conn) 
{
 String file_m_path = "";
 String str  = "select file_path from bug_svn_m_file where bugid='"+bugid+"' and file_name='"+file_m+"'"; 
 
 Statement stmt =  null;

 try 
 {
		stmt =  conn.createStatement();
        stmt.execute(str);
        ResultSet rs = stmt.getResultSet();
        while(rs.next())
        {
        	
        	file_m_path=rs.getString("file_path");
        	break;
     	
        }//wh
    
        if(rs!=null)
        	rs.close();
        if(stmt!=null)
        	stmt.close();
	} catch (SQLException e) 
	{ 	e.printStackTrace(); }
 
 return file_m_path;
}

// To delete data from table.
private void reset(Connection conn) 
{
	Statement stmt =  null;
	String str = "delete from h1_accuracy_1";
	
	try 
	{
		stmt= conn.createStatement();
	    stmt.execute(str);
	} catch (SQLException e) 
	{ e.printStackTrace(); 	}
	
}

public void intr()
{
	 BufferedReader br =  new BufferedReader(new InputStreamReader(System.in));
	 System.out.println("INTR");
	 try {
		br.readLine();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	 
}

public static void main(String args[])
 {
   
	h1_accuracy h1=  new h1_accuracy();
	db_conn db  =  new db_conn();
	h1.conn =  db.connect(h1.dbname, h1.userid, h1.passwd);
	h1.reset(h1.conn);//@@Not a comment
	h1.No_of_bugids=72;
	h1.acc_1(h1.conn,50);//@@Not a comment
	h1.calculate_total_accuracy1(h1.conn);
	h1.cal_total_acc2(h1.conn);
	db.close(h1.conn);
      
 }//main


	
}//class
